package com.itheima.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.pojo.vo.PageBean;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.mapper.ExprMapper;
import com.itheima.tlias.pojo.dto.SelectEmpDTO;
import com.itheima.tlias.pojo.dto.SelectempDetailDTO;
import com.itheima.tlias.pojo.entity.Expr;
import com.itheima.tlias.pojo.po.InsertEmpParam;
import com.itheima.tlias.pojo.po.SelectEmpPageParam;
import com.itheima.tlias.pojo.po.UpdateEmpParam;
import com.itheima.tlias.service.EmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private ExprMapper exprMapper;

    @Override
    public PageBean selectEmpByPageParam(SelectEmpPageParam param) {
        /*if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())){
            return new PageBean();
        }
        param.setPage((param.getPage() - 1)*param.getPageSize());*/
        //注意 pageHelper 会自动执行一个count sql ，并且会自动拼接limt
        //因此必须写在自己desql之前
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectEmpDTO> list = empMapper.selectEmpByPageParam(param);
        /*param.setPage(null);
        param.setPageSize(null);
        long size = empMapper.selectEmpByPageParam(param).size();*/
        PageBean<SelectEmpDTO> pageBean = new PageBean();

        pageBean.setTotal(page.getTotal());
        pageBean.setRows(list);
        return pageBean;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmp(InsertEmpParam param) {

        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        log.info("员工回显的主键，{}", param.getId());
        int flag = empMapper.insertEmp(param);
        //插入工作履历
        List<Expr> exprList = param.getExprList();

        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList = exprList.stream().map(expr -> {
                expr.setId(param.getId());
                expr.setUpdateTime(LocalDateTime.now());
                expr.setCreateTime(LocalDateTime.now());
                return expr;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历,{}", i);
        }
        return flag;
    }
    @Transactional(rollbackFor = Exception.class)

    @Override
    public int batchDeleteEmpByIds(List<Integer> ids) {
        int empRows = empMapper.batchDeleteEmpByIds(ids);
        int exprRows = exprMapper.batchDeleteExprByEmpIds(ids);
        return empRows;
    }
    @Override
    public SelectempDetailDTO selectEmpById(Integer id) {
        SelectempDetailDTO dto = empMapper.selectEmpById(id);
        List<Expr> exprs = exprMapper.selectExprByEmpId(id);
        dto.setExprList(exprs);
        return dto;

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmpParam param) {
        param.setUpdateTime(LocalDateTime.now());
        int empRows = empMapper.updateEmp(param);
        int exprDelRows = exprMapper.batchDeleteExprByEmpIds(List.of(param.getId()));
        log.info("删除{}条旧履历数据",exprDelRows);

        //插入工作履历
        List<Expr> exprList = param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList = exprList.stream().map(expr -> {
                expr.setId(param.getId());
                expr.setUpdateTime(LocalDateTime.now());
                expr.setCreateTime(LocalDateTime.now());
                return expr;
            }).toList();
            int exprRows = exprMapper.batchInsertExpr(exprList);
            log.info("插入新员工履历,{}", exprRows);
        }
        return empRows;
    }

    @Override
    public List<SelectempDetailDTO> selectEmpAll() {
        return exprMapper.selectEmpAll();
    }
}
